package com.qyt.hotel.controller;

import com.qyt.hotel.config.GlobalProperties;
import com.qyt.hotel.entity.Result;
import com.qyt.hotel.entity.RoomImg;
import com.qyt.hotel.entity.StatusCode;
import com.qyt.hotel.service.RoomImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/roomImg")
public class RoomImgController {

    @Resource
    private RoomImgService roomImgService;

    @Autowired
    private GlobalProperties globalProperties;

    @GetMapping("/list")
    public Result getRoomImgList(@RequestParam("roomId") String roomId) {
        List<RoomImg> roomImgList = roomImgService.getRoomImgList(roomId);
        if (!roomImgList.isEmpty()) {
            return new Result("查询成功", true, StatusCode.OK, roomImgList);
        } else {
            return new Result("查询失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/uploadImg")
    public Result uploadRoomImg(@RequestParam("file") MultipartFile file) {
        // 获得原始文件名+格式
        String OriginalFileName = file.getOriginalFilename();
        System.out.println(OriginalFileName);
        if (null != OriginalFileName) {
            // 截取文件名
            String fileName = OriginalFileName.substring(0, OriginalFileName.lastIndexOf("."));
            // 截取文件格式
            String fileType = OriginalFileName.substring(OriginalFileName.lastIndexOf("."));
            //获取当前时间(精确到毫秒)
            Date date = new Date();
            String formatDate = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(date);
            //原文件名+当前时间作为新文件名
            String fileNewName = formatDate + (new Random()).nextInt(10000) + fileType;
            File f = new File(globalProperties.getRoomStaticPath());
            if (!f.exists()) {
                boolean mkdirs = f.mkdirs();
            }
            if (!file.isEmpty()) {
                try {
                    FileOutputStream fos = new FileOutputStream(globalProperties.getRoomStaticPath() + fileNewName);
                    InputStream in = file.getInputStream();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len = in.read(bytes)) != -1) {
                        fos.write(bytes, 0, len);
                    }
                    fos.close();
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    return new Result("上传异常", false, null, null);
                }
            }
            return new Result("上传成功", true, null, fileNewName);
        } else {
            return new Result("上传失败", false, null, null);
        }
    }

    @PostMapping("/add")
    public Result addRoomImg(@RequestParam("roomId") String roomId, @RequestParam("roomImgName") String roomImgName) {
        Integer count = roomImgService.addRoomImg(roomId, roomImgName);
        // 获取新插入的图片的roomImgId
        String roomImgId = roomImgService.getRoomImgId(roomImgName);
        if (null != count) {
            return new Result("添加成功", true, StatusCode.OK, roomImgId);
        } else {
            return new Result("添加失败", false, StatusCode.ERROR, null);
        }

    }

    @PostMapping("/delete")
    public Result deleteRoomImg(@RequestParam("roomImgId") Integer roomImgId, @RequestParam("fileName") String fileName) {
        Integer count = roomImgService.deleteRoomImg(roomImgId);
        File f = new File(String.format("%s%s", globalProperties.getRoomStaticPath(), fileName));
        boolean flag = f.delete();
        if (null != count) {
            return new Result("删除成功", true, StatusCode.OK, null);
        } else {
            return new Result("删除失败", false, StatusCode.ERROR, null);
        }
    }
}
